<?php	
	include('db_connection.php');

	class add_course{
		function new_ebook_add(){				
			
			$insert_trainer = "INSERT into tbl_ebook (ebook_name,ebook_fee,ebook_pwd,ebook_description,ebook_commission,ebook_is_active) VALUES ('".addslashes($_POST['ebook_name'])."', '".addslashes($_POST['ebook_fee'])."', '".$_POST['ebook_pwd']."','".addslashes($_POST['ebook_description'])."','".addslashes($_POST['ebook_cmsn'])."', '".$_POST['is_active']."')";
			
			$trainer_query = mysql_query($insert_trainer) or die(mysql_error());
			
			$last_id = mysql_insert_id();

			$uploadimgdir = '../dashboard/ebook_files/'.$last_id.'/';
			$isimgdir = '../dashboard/ebook_files/'.$last_id;

			if(!empty($_FILES['ebook_file']['name'])){
				if(is_dir($isimgdir)){
					$files = glob( $uploadimgdir . '*', GLOB_MARK );       
					foreach( $files as $file )
					{
						unlink( $file );       
					}
					rmdir($isimgdir);  
				}
				if (!is_dir($isimgdir)) {
					mkdir($isimgdir);         
				}
				
				$temp_img = explode(".", $_FILES['ebook_file']['name']);
				$extension_img = end($temp_img);
				$newfilename_img = $last_id.'.'.end($temp_img);
				$uploadfile_img = $uploadimgdir.$newfilename_img;
				if (move_uploaded_file($_FILES['ebook_file']['tmp_name'], $uploadfile_img)) {
					$upload_file ="UPDATE tbl_ebook SET ebook_file = '".$_FILES['ebook_file']['name']."' WHERE id='".$last_id."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}

			$uploadebkimgdir = '../dashboard/ebook_images/'.$last_id.'/';
			$isebkimgdir = '../dashboard/ebook_images/'.$last_id;

			if(!empty($_FILES['ebook_image']['name'])){
				if(is_dir($isebkimgdir)){
					$files = glob( $uploadebkimgdir . '*', GLOB_MARK );       
					foreach( $files as $file )
					{
						unlink( $file );       
					}
					rmdir($isebkimgdir);  
				}
				if (!is_dir($isebkimgdir)) {
					mkdir($isebkimgdir);         
				}
				
				$temp_img = explode(".", $_FILES['ebook_image']['name']);
				$extension_img = end($temp_img);
				$newfilename_img = $last_id.'.'.end($temp_img);
				$uploadfile_img = $uploadebkimgdir.$newfilename_img;
				if (move_uploaded_file($_FILES['ebook_image']['tmp_name'], $uploadfile_img)) {
					$upload_file ="UPDATE tbl_ebook SET ebook_image = '".$_FILES['ebook_image']['name']."' WHERE id='".$last_id."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}
			$success_msg = "Ebook Has been Sucessfully Added";
			return $success_msg;
		}

		function get_ebook($id){
			$select = "SELECT * FROM tbl_ebook WHERE id='".$id."'";
			$query = mysql_query($select) or die(mysql_error());
			
			if(mysql_num_rows($query)){
				$results = array();
				while($row = mysql_fetch_array($query)){
					foreach ($row as $k=>$v){
					  $result->$k = $v;
					}
				 
					$results[] = $result;
				}
				return $results;    
			}
		}

		function update_ebook(){	
			$update = "UPDATE tbl_ebook SET ebook_name = '".addslashes($_POST['ebook_name'])."',
											ebook_fee = '".addslashes($_POST['ebook_fee'])."',
											ebook_pwd = '".$_POST['ebook_pwd']."',
											ebook_description = '".addslashes(trim($_POST['ebook_description']))."',
											ebook_is_active = '".$_POST['is_active']."',
											ebook_commission = '".addslashes($_POST['ebook_cmsn'])."',											
											end_ts = now()
											WHERE id='".$_POST['ebook_id']."'";
			$query_update = mysql_query($update) or die(mysql_error());			

			$select = "SELECT * FROM tbl_ebook where id='".$_POST['ebook_id']."'";
			$query = mysql_query($select) or die(mysql_error());
			
			$count = 0;
			if(mysql_num_rows($query)>0){
				$count = mysql_num_rows($query);
			}
			$uploadimgdir = '../dashboard/ebook_files/'.$_POST['ebook_id'].'/';
			$isimgdir = '../dashboard/ebook_files/'.$_POST['ebook_id'];
			if(!empty($_FILES['ebook_file']['name'])){
				if(is_dir($isimgdir)){
					$files = glob( $uploadimgdir . '*', GLOB_MARK );       
					foreach( $files as $file )
					{
						unlink( $file );       
					}
					rmdir($isimgdir); 
				}
				if (!is_dir($isimgdir)) {
					mkdir($isimgdir);         
				}
				
				$temp_img = explode(".", $_FILES['ebook_file']['name']);
				$extension_img = end($temp_img);
				$newfilename_img = $_POST['ebook_id'].'.'.end($temp_img);
				$uploadfile_img = $uploadimgdir.$newfilename_img;
				if (move_uploaded_file($_FILES['ebook_file']['tmp_name'], $uploadfile_img)) {
					$upload_file ="UPDATE tbl_ebook SET ebook_file = '".$_FILES['ebook_file']['name']."',end_ts = now() WHERE id='".$_POST['ebook_id']."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}

			$uploadebkimgdir = '../dashboard/ebook_images/'.$_POST['ebook_id'].'/';
			$isebkimgdir = '../dashboard/ebook_images/'.$_POST['ebook_id'];

			if(!empty($_FILES['ebook_image']['name'])){
				if(is_dir($isebkimgdir)){
					$files = glob( $uploadebkimgdir . '*', GLOB_MARK );       
					foreach( $files as $file )
					{
						unlink( $file );       
					}
					rmdir($isebkimgdir);  
				}
				if (!is_dir($isebkimgdir)) {
					mkdir($isebkimgdir);         
				}
				
				$temp_img = explode(".", $_FILES['ebook_image']['name']);
				$extension_img = end($temp_img);
				$newfilename_img = $_POST['ebook_id'].'.'.end($temp_img);
				$uploadfile_img = $uploadebkimgdir.$newfilename_img;
				if (move_uploaded_file($_FILES['ebook_image']['tmp_name'], $uploadfile_img)) {
					$upload_file ="UPDATE tbl_ebook SET ebook_image = '".$_FILES['ebook_image']['name']."' WHERE id='".$_POST['ebook_id']."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}

			$success_msg = "Ebook Has been Sucessfully Updated";
			return $success_msg;
		}
		
		function update_ebook_commission(){	
			$update = "UPDATE tbl_ebook SET ebook_commission = '".addslashes($_POST['ebook_commission'])."',
											end_ts = now()
											WHERE id='".$_POST['ebook_id']."'";
			$query_update = mysql_query($update) or die(mysql_error());	

			$success_msg = "Ebook Commission Has been Sucessfully Updated";
			return $success_msg;
		}

		function del_ebook($course_id){
			$del_update = "UPDATE tbl_ebook SET ebook_is_active = '0', end_ts = now() WHERE id='".$course_id."'";			
			$query_update = mysql_query($del_update) or die(mysql_error());
			$success_msg = "Ebook Has been Sucessfully Inactive";
			return $success_msg;			
		}

	}
?>